package PoLan;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class Polanation {
    public static void main(String[] args) {
        //String suffixExpression = "4 5 * 8 - 60 + 8 2 / +";
        String experssion = "1+((2+3)*4)-5";
        //List<String> list=list(suffixExpression);
        //System.out.println(list);
        //int res=calculate(list);
        //System.out.println(res);
        System.out.println(toInfixExperssionList(experssion));
        System.out.println(parseSuffixExpersion(toInfixExperssionList(experssion)));
        System.out.println(calculate(parseSuffixExpersion(toInfixExperssionList(experssion))));
    }
    public static List<String> parseSuffixExpersion(List<String> ls) {
        A a = new A();
        List<String> list = new ArrayList<>();
        Stack<String> stack = new Stack<>();
        for (String item : ls) {
            if (item.matches("\\d+")) {
                list.add(item);
            } else if (item.equals("(")) {
                stack.push(item);
            } else if (item.equals(")")) {
                while (!stack.peek().equals("(")) {
                    list.add(stack.pop());
                }
                stack.pop();
            }else {
                while (stack.size()!=0&&(A.getValue(stack.peek())>A.getValue(item))){
                    list.add(stack.pop());
                }
                stack.push(item);
            }
        }
        while (stack.size()!=0){
            list.add(stack.pop());
        }
        return list;
    }

    public static List<String> toInfixExperssionList(String s){
        List<String> list=new ArrayList<>();
        int i=0;
        String str;
        char ch;
        do {
            if ((ch=s.charAt(i))<48||(ch=s.charAt(i))>57){
                list.add(ch+"");
                i++;
            } else{
                str="";
                while (i<s.length()&&(ch=s.charAt(i))>=48&&(ch=s.charAt(i))<=57) {
                   str+=ch;
                   i++;
            }
                list.add(str);
            }
        }while (i<s.length());
        return list;
    }


    public static List<String> list(String suffixExpression){
        List<String> list=new ArrayList<>();
        String[] spit=suffixExpression.split(" ");
        for (String s:spit) {
                list.add(s);
        }
        return list;
    }

    public static int calculate(List<String> ls){
        Stack<String> stack=new Stack<String>();
        for (String l: ls) {
           if (l.matches("\\d+")){
               stack.push(l);
           }else {
               int num1=Integer.parseInt(stack.pop());
               int num2=Integer.parseInt(stack.pop());
               int res=0;
               if (l.equals("+")){
                   res=num1+num2;
               } else if (l.equals("-")) {
                   res=num2-num1;
               } else if (l.equals("*")) {
                   res=num1*num2;
               } else if (l.equals("/")) {
                   res=num2/num1;
               }else {
                   throw new RuntimeException("运算有误");
               }
               stack.push(""+res);
           }
        }
        return Integer.parseInt(stack.pop());
    }
}
class A{
    private static int add=1;
    private static int sub=1;
    private static int mul=2;
    private static int div=2;
    public static int  getValue(String operation){
        int result= 0;
        switch (operation){
            case "+":
                result = add;
                break;
            case "-":
                result = sub;
                break;
            case "*":
                result = mul;
                break;
            case "/":
                result = div;
                break;
            default:
                //System.out.println("不存在该运算符");
                break;
        }
        return result;
    }
}